//Does most of the calculations for drawing -- as per this http://www.p01.org/articles/DHTML_techniques/Drawing_lines_in_JavaScript/
//dependendent on the returnImage() function that is created dynamically to return the embedded gifs for drawing

function drawParentLine(parentId, dependentIds, Ax, Ay) 
{ 
    if (dependentIds != '') 
    { 
        var getDependencies = dependentIds.split(','); 
        for (d = 0; d < getDependencies.length; d++) 
        { 
            var getArrow = document.getElementById(parentId + 'ParentOf' + getDependencies[d] + 'Arrow');
            var objectHandle = document.getElementById(parentId + 'ParentOf' + getDependencies[d]); 
            var checkChild = document.getElementById(getDependencies[d] + 'ChildOf' + parentId); 
            var checkChildArrow = document.getElementById(getDependencies[d] + 'ChildOf' + parentId + 'Arrow'); 
            if (checkChild) { checkChild.style.visibility='hidden'; }
            if (checkChildArrow) { checkChildArrow.style.visibility='hidden'; }
            
            if (objectHandle) 
            { 
                var gDependency = document.getElementById(getDependencies[d]);
                if (gDependency)
                {
                    var Bx = gDependency.style.left; 
                    var By = gDependency.style.top;
                    Bx = parseInt(Bx.replace('px','')); By = parseInt(By.replace('px','')); 

                    updateLine(objectHandle, Ax, Ay, Bx, By, getArrow); 
                }
            } 
        } 
    } 
} 

function drawChildLine(childId, dependentIds, Bx, By) 
{ 
    if (dependentIds != '') 
    { 
        var getDependencies = dependentIds.split(','); 
        for (d = 0; d < getDependencies.length; d++) 
        { 
            var getArrow = document.getElementById(childId + 'ChildOf' + getDependencies[d] + 'Arrow');  
            var objectHandle = document.getElementById(childId + 'ChildOf' + getDependencies[d]); 
            var checkParent = document.getElementById(getDependencies[d] + 'ParentOf' + childId);
            var checkParentArrow = document.getElementById(getDependencies[d] + 'ParentOf' + childId + 'Arrow'); 
            if (checkParent) { checkParent.style.visibility='hidden'; } 
            if (checkParentArrow) { checkParentArrow.style.visibility='hidden'; }

            if (objectHandle)
            { 
                var gDependency = document.getElementById(getDependencies[d]);
                if (gDependency)
                {
                    var Ax = gDependency.style.left;
                    var Ay = gDependency.style.top;                 
                    Ax = parseInt(Ax.replace('px','')); Ay = parseInt(Ay.replace('px','')); 

                    updateLine(objectHandle, Ax, Ay, Bx, By, getArrow); 
                }
            } 
        } 
    } 
} 


//window.onerror = handleErr; 
//function handleErr(msg,url,l) 
//{ 
//    window.status = msg + ' | ' + url + ' | ' + l; 
//    return true
//}

function updateLine(objectHandle, Ax, Ay, Bx, By, arrowHandle) 
{
    objectHandle.style.visibility='visible'; 
    arrowHandle.style.visibility='visible';
    
    var
    xMin		= Math.min( Ax, Bx ),
    yMin		= Math.min( Ay, By ),
    xMax		= Math.max( Ax, Bx ),
    yMax		= Math.max( Ay, By ),
    boxWidth	= Math.max( xMax-xMin, 1 ),
    boxHeight	= Math.max( yMax-yMin, 1 ),
    tmp		= Math.min( boxWidth, boxHeight ),
    smallEdge	= 1,
    newSrc
    while( tmp>>=1 )
    smallEdge<<=1
    var dir = '';    
    if ( (Bx - Ax) * (By - Ay) < 0) { dir = 'up'; } else { dir = 'down'; } 
    newSrc = returnImage(smallEdge, dir); 
    
    if( objectHandle.src.indexOf( newSrc )== -1 )
        objectHandle.src = newSrc
    
    with( objectHandle.style )
    {
        //window.status = window.status +  ' Ax: ' + Ax + ' | Ay: ' + Ay + ' | Bx: ' + Bx + ' By: ' + By + ' | bWidth: ' + boxWidth + ' | bHeight: ' + boxHeight + ' xMin: ' + xMin + ' | yMin: ' + yMin; 
        width	= boxWidth	+ "px" 
        height	= boxHeight	+ "px" 
        left	= xMin		+ "px" 
        top	= yMin	+ "px" 
    }
   
    if (Ay - By > 0)
    {
        arrowHandle.src = returnArrow('up');
    }
    else
    {
        arrowHandle.src = returnArrow('down');
    }
    with ( arrowHandle.style )
    {
        top = (Ay - 15) + "px";
        left = (Ax - 15) + "px";
    }
} 

function MoveNode(parentId, dependentParentIds, dependentChildIds, XDif, YDif)
{
    //move the div itself
    document.getElementById(parentId).style.left = (parseInt(document.getElementById(parentId).style.left.replace('px','')) + XDif) + 'px';
    document.getElementById(parentId).style.top = (parseInt(document.getElementById(parentId).style.top.replace('px','')) + YDif) + 'px';
    
    /*        
    //move the dependencies
    var getDependencies = dependentParentIds.split(','); 
    for (d = 0; d < getDependencies.length; d++) 
    { 
        //move lines and arrows this node is parent to
        var checkChild = document.getElementById(parentId + 'ChildOf' + getDependencies[d]); 
        var checkChildArrow = document.getElementById(parentId + 'ChildOf' + getDependencies[d] + 'Arrow');
        
        if (checkChild) { checkChild.style.top = (parseInt(checkChild.style.top.replace('px','')) + YDif) + "px";
                           checkChild.style.left = (parseInt(checkChild.style.left.replace('px','')) + XDif) + "px"; }
        
        if (checkChildArrow) { checkChildArrow.style.top = (parseInt(checkChildArrow.style.top.replace('px','')) + YDif) + "px";
                                checkChildArrow.style.left = (parseInt(checkChildArrow.style.left.replace('px','')) + XDif) + "px"; }
                                
        //move child
        var checkChild = document.getElementById(getDependencies[d] + 'ParentOf' + parentId); 
        var checkChildArrow = document.getElementById(getDependencies[d] + 'ParentOf' + parentId + 'Arrow'); 
        if (checkChild) { checkChild.style.top = (parseInt(checkChild.style.top.replace('px','')) + YDif) + "px";
                           checkChild.style.left = (parseInt(checkChild.style.left.replace('px','')) + XDif) + "px"; } 
                           
        if (checkChildArrow) { checkChildArrow.style.top = (parseInt(checkChildArrow.style.top.replace('px','')) + YDif) + "px";
                                checkChildArrow.style.left = (parseInt(checkChildArrow.style.left.replace('px','')) + XDif) + "px"; }
    }
    
    getDependencies = dependentChildIds.split(','); 
    for (d = 0; d < getDependencies.length; d++) 
    { 
        //move lines and arrows this node is parent to
        var checkParent = document.getElementById(parentId + 'ParentOf' + getDependencies[d]); 
        var checkParentArrow = document.getElementById(parentId + 'ParentOf' + getDependencies[d] + 'Arrow');
        
        if (checkParent) { checkParent.style.top = (parseInt(checkParent.style.top.replace('px','')) + YDif) + "px";
                           checkParent.style.left = (parseInt(checkParent.style.left.replace('px','')) + XDif) + "px"; }
        
        if (checkParentArrow) { checkParentArrow.style.top = (parseInt(checkParentArrow.style.top.replace('px','')) + YDif) + "px";
                                checkParentArrow.style.left = (parseInt(checkParentArrow.style.left.replace('px','')) + XDif) + "px"; }
                                
        //move child
        var checkChild = document.getElementById(getDependencies[d] + 'ChildOf' + parentId); 
        var checkChildArrow = document.getElementById(getDependencies[d] + 'ChildOf' + parentId + 'Arrow'); 
        if (checkChild) { checkChild.style.top = (parseInt(checkChild.style.top.replace('px','')) + YDif) + "px";
                           checkChild.style.left = (parseInt(checkChild.style.left.replace('px','')) + XDif) + "px"; } 
                           
        if (checkChildArrow) { checkChildArrow.style.top = (parseInt(checkChildArrow.style.top.replace('px','')) + YDif) + "px";
                                checkChildArrow.style.left = (parseInt(checkChildArrow.style.left.replace('px','')) + XDif) + "px"; }
                                
    }
    */

}